import { TAllTypes, IValue, IValueNumber } from '../../types';
import { AFunc, IPositionValue } from '../types';
import { calculate } from '../../calculate';
export class Sum extends AFunc {
  protected calculateArr(arr: IValue[]): IValueNumber {
    let result: IValueNumber = {type: 'value', dataType: 'number', value: 0};
    for(let item of arr) {
      if(item && item.type === 'value' && ((item.dataType === 'string' && !isNaN(Number(item.value))) || item.dataType === 'number')) {
        let num = Number(item.value);
        result = calculate('+', [result, {type: 'value', dataType: 'number', value: num}]) as IValueNumber;
      }
    }
    return result;
  }
  public excute(args: TAllTypes[]): TAllTypes[] {
    let arr: IValue[] = [];
    for(let item of args) {
      if(item.type === 'value' && (item.dataType === 'number' || item.dataType === 'string')) {
        arr.push(item);
        continue;
      }
      if(item.type === 'range') {
        let items = item.value.getValues();
        arr = arr.concat(items as any);
        continue;
      }
      if(item.type === 'position' || item.type === 'rowPosition' || item.type === 'colPosition') {
        let items = this.positionVal.getValue(item);
        arr = arr.concat(items);
      }
    }
    let vals = this.calculateArr(arr);
    return [vals];
  }
}
